<include file="Common/header-control" />
<div class="row">
    <div class="col-md-12">
        <ul class="nav nav-tabs">
            <li><a href="{:U('control/platform/alipay')}">服务窗列表</a></li>
            <if condition="!empty($entity)">
                <li class="active"><a href="{:U('control/platform/alipay?do=modify&id=' . $entity['id'])}">编辑服务窗账号</a></li>
                <li><a href="{:U('control/platform/alipay?do=create')}">添加服务窗账号</a></li>
                <else/>
                <li class="active"><a href="{:U('control/platform/alipay?do=create')}">添加服务窗账号</a></li>
            </if>
        </ul>
    </div>
    <script>
        require(['jquery', 'util'], function($, u){
            $(function(){
                $('#theform').submit(function(){
                    if($.trim($('input[name="title"]').val()) == '') {
                        u.message('必须输入服务窗名称');
                        return false;
                    }
                    <if condition="empty($entity)">
                        if($.trim($('input[name="qr"]').val()) == '') {
                            u.message('必须上传服务窗二维码');
                            return false;
                        }
                    </if>
                });
            });
        });
    </script>
    <form id="theform" action="" method="post" class="form-horizontal" role="form" enctype="multipart/form-data">
    <div class="box col-md-12">
        <div class="box-inner">
            <div class="box-header well" data-original-title="">
                <if condition="!empty($entity)">
                    <h2><i class="fa fa-edit"></i> 编辑支付宝服务窗账号</h2>
                    <else/>
                    <h2><i class="fa fa-list"></i> 添加支付宝服务窗账号</h2>
                </if>
            </div>
            <div class="box-content">
                <div class="form-group">
                    <label class="col-sm-2 control-label">服务窗名称</label>
                    <div class="col-sm-4">
                        <input class="form-control" type="text" name="title" value="{$entity['title']}"/>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">备注</label>
                    <div class="col-sm-4">
                        <textarea name="remark" class="form-control" rows="4">{$entity['remark']}</textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">服务窗二维码</label>
                    <div class="col-sm-5">
                        <if condition="!empty($entity['qr'])">
                            <input type="file" name="qr" value="" accept="image/*" data-preview="{:attach($entity['qr'])}" data-caption="当前服务窗二维码">
                            <else/>
                            <input type="file" name="qr" value="" accept="image/*">
                        </if>
                        <script>
                            require(['util'], function(u){u.preview();});
                        </script>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-1">
                        <if condition="empty($entity)">
                            <button type="submit" class="btn btn-block btn-primary">新增</button>
                            <else/>
                            <button type="submit" class="btn btn-block btn-primary">保存</button>
                        </if>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <if condition="!empty($entity)">
        <div class="box col-md-12">
            <div class="box-inner">
                <div class="box-header well" data-original-title="">
                    <h2><i class="fa fa-plug"></i> 接入支付宝服务窗</h2>
                </div>
                <div class="box-content">
                    <script>
                        require(['jquery', 'util'], function($, u){
                            $(function(){
                                var trimKey = function(key) {
                                    key = key.replace(/-----BEGIN PUBLIC KEY-----/g, '');
                                    key = key.replace(/-----END PUBLIC KEY-----/g, '');
                                    key = key.replace(/[\r\n]/g, '');
                                    return key;
                                };
                                u.clip($('#copy-key')[0], function(){
                                    return $('#view-public-key').val();
                                }, function() {
                                    u.message('成功复制商家公钥, 请访问支付宝平台进行接入 <br /><if condition="!$isGen"><strong>注意: 接入之前请保存密钥对.</strong></if>');
                                });
                                $('#gen-key').click(function(){
                                    $.post(location.href, {method: 'generate'}).success(function(dat){
                                        var json = $.parseJSON(dat);
                                        if(u.is_error(json)) {
                                            u.message(json.message);
                                            return;
                                        }
                                        $('#view-public-key').val(trimKey(json.public));
                                    });
                                });
                                $('#view-public-key').val(trimKey($('#public_key').val()));
                                if($('#public_key').val() == '') {
                                    $('#gen-key').trigger('click');
                                }
                                $('#copy-url abbr').html(location.protocol + '//' + location.host + $('#copy-url abbr').text());
                                u.clip($('#copy-url')[0], $('#copy-url').text());
                            });
                        });
                    </script>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">接入URL</label>
                        <div class="col-sm-4">
                            <p class="form-control-static">
                                <a href="javascript:;" id="copy-url"><abbr title="点击复制">{:U('/api/' . $entity['id'])}</abbr></a>
                            </p>
                            <div class="help-block">
                                点击链接复制
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">商户公钥</label>
                        <div class="col-sm-6">
                            <input id="public_key" type="hidden" value="{$entity['public_key']}" />
                            <textarea id="view-public-key" class="form-control" rows="4" readonly></textarea>
                            <div class="help-block">
                                您的系统支持在线生成密钥对, 您可以直接复制密钥字符串, 保存至支付宝接口进行接入
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-2">
                            <button id="gen-key" type="button" class="btn btn-block btn-default">重新生成公钥</button>
                        </div>
                        <div class="col-sm-2">
                            <button id="copy-key" type="button" class="btn btn-block btn-default">复制公钥字符串用于接入</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </if>
    </form>
</div>
<include file="Common/footer-control" />
